//
//  ViewController.m
//  Example
//
//  Created by apang on 2022/9/12.
//

#import "ViewController.h"
#import <APMBanner/APMBanner.h>
#import <SDWebImage/SDWebImage.h>
@interface ViewController ()<APMBannderViewDelegate>
@property (nonatomic, strong) APMBannerView * bannerView;
@property (nonatomic, copy) NSArray * images;
@property (nonatomic, copy) NSArray * current;
@property (nonatomic, copy) NSArray * images2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _bannerView = [[APMBannerView alloc] initWithFrame:CGRectMake(0, 100, 375, 200)];
    _bannerView.delegate = self;
    [_bannerView registerClass:[APMBannerViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_bannerView];
    
    UILabel * titleLable1 = [self title:@"scrollEnable" view:self.bannerView];
    UISwitch * switchControl1 = [self switchControl:titleLable1];
    switchControl1.tag = 10;
    
    UILabel * titleLable2 = [self title:@"autoScrollEnable" view:titleLable1];
    UISwitch * switchControl2 = [self switchControl:titleLable2];
    switchControl2.tag = 11;
    
    UILabel * titleLable3 = [self title:@"scrollPosition" view:titleLable2];
    UISwitch * switchControl3 = [self switchControl:titleLable3];
    switchControl3.tag = 12;
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"reloadData" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(testBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[button.leftAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.leftAnchor]];
    [self.view addConstraint:[button.topAnchor constraintEqualToAnchor:titleLable3.bottomAnchor constant:20]];
    
    _images = @[
        @"http://gaga-images.paixin.com/photos2/2021/0605/60ba5aa96b42c.jpeg?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0605/60ba5aa712049.jpeg?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0522/60a8f9e6cce31.jpeg?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0504/6090bddc947f3.jpeg?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0416/6078e71e327fe.jpeg-7mx_image2water?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0416/6078e7207c53b.jpeg-7mx_image2water?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0416/6078e723ad8e5.jpeg-7mx_image2water?imageView2/2/w/632/h/389",
        @"http://gaga-images.paixin.com/photos2/2021/0406/606c5fa41297a.jpeg?imageView2/2/w/632/h/389"
    ];
    
    _images2 = @[
        @"http://gaga-images.paixin.com/photos2/2021/0304/6040da6d01128.jpeg?imageView2/2/w/600/h/1000",
        @"http://gaga-images.paixin.com/photos2/2020/0524/5ec9e631e57c5.jpeg-7mx_image2water?imageView2/2/w/600/h/1000",
        @"http://gaga-images.paixin.com/photos2/2021/0226/603871e63eb6f.jpeg?imageView2/2/w/600/h/1000",
        @"http://gaga-images.paixin.com/photos2/2020/1126/5fbe8d36af9f2.jpeg?imageView2/2/w/600/h/1000",
        @"http://gaga-images.paixin.com/photos2/2019/1114/5dcd51c5e02a3.jpeg-7mx_image2water?imageView2/2/w/600/h/1000"
    ];
    _current = _images;
}

- (UISwitch *)switchControl:(UILabel *)label {
    UISwitch * switchControl = [[UISwitch alloc] init];
    switchControl.on = YES;
    [switchControl addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    switchControl.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:switchControl];
    [self.view addConstraint:[switchControl.leftAnchor constraintEqualToAnchor:label.rightAnchor constant:20]];
    [self.view addConstraint:[switchControl.centerYAnchor constraintEqualToAnchor:label.centerYAnchor]];
    return switchControl;
}

- (UILabel *)title:(NSString *)title view:(UIView *)view {
    UILabel * label = [[UILabel alloc] init];
    label.text = title;
    label.textColor = [UIColor blackColor];
    label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:label];
    [self.view addConstraint:[label.leftAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.leftAnchor]];
    [self.view addConstraint:[label.topAnchor constraintEqualToAnchor:view.bottomAnchor constant:20.0]];
    return label;
}

- (void)testBtnOnClick:(UIButton *)sender {
    _current = _current == _images ? _images2 : _images;
    [self.bannerView reloadData];
}

- (void)switchValueChanged:(UISwitch *)sender {
    switch (sender.tag) {
        case 10:
            self.bannerView.scrollEnable = sender.on;
            break;
        case 11:
            self.bannerView.autoScrollEnable = sender.on;
            break;
        case 12:
            self.bannerView.scrollPosition = sender.on ? APMBannerViewScrollPositionLeft : APMBannerViewScrollPositionRight;
            break;
            
        default:
            break;
    }
}

//MARK: - APMBannderViewDelegate

- (NSInteger)numberOfItemsInBannerView:(APMBannerView *)bannerView {
    return self.current.count;
}

- (APMBannerViewCell *)bannerView:(APMBannerView *)bannerView cellForItemAtIndexPath:(NSIndexPath *)indexPath itemIndex:(NSInteger)itemIndex {
    APMBannerViewCell * cell = [bannerView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    NSURL * url = [NSURL URLWithString:self.current[itemIndex]];
    NSLog(@"indexPath:%ld index:%ld  url:%@",(long)indexPath.row, (long)itemIndex, url);
    [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Wu"]];
    return cell;
}

- (void)bannerView:(APMBannerView *)bannerView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"_index:%ld",index);
}

-(void)bannerView:(APMBannerView *)bannerView didShowItemAtIndex:(NSInteger)index {
    NSLog(@"_index:%ld",index);
}
@end
